﻿using Masuit.Tools.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceTaskPlain
    {


        EFYihongIotContext ef = new EFYihongIotContext();
        public iot_device_task_plain getPlain(string guid)
        {
            return ef.iot_device_task_plain.Where(c => c.tp_guid == guid).FirstOrDefault();
        }
        public void delPlain(string tpguid)
        {
            ef.iot_device_task_plain.Where(c => c.tp_guid == tpguid).ExecuteDelete();
        }

        public List<iot_device_task_plain> getPlainList(string cguid)
        {
            return ef.iot_device_task_plain.Where(c => c.tp_status == 0 && c.tp_c_guid == cguid).ToList();
        }

        public List<iot_device_task_plain> getPlainByNotProductList()
        {
            return ef.iot_device_task_plain.Where(c => c.tp_status == 0 && c.tp_is_template == 0).ToList();
        }
        public List<iot_device_task_plain> getPlainByProducAllList(string p_guid)
        {
            return ef.iot_device_task_plain.Where(c =>  c.tp_p_guid == p_guid).ToList();
        }
        public List<iot_device_task_plain> getPlainByProductList(string p_guid)
        {
            return ef.iot_device_task_plain.Where(c => c.tp_status == 0 && c.tp_p_guid == p_guid).ToList();
        }
        public void setPlainStatus(string pguid, int modelno, int status)
        {
            ef.iot_device_task_plain.Where(c => c.tp_p_guid == pguid && c.tp_p_model_no == modelno && c.tp_p_model_no != null).ExecuteUpdate(c => c.SetProperty(p => p.tp_status, status));

            if (status == 1)
            {
                //1禁用 0启用

                //删除未完成的流程和条件
                iot_device_task_plain item = ef.iot_device_task_plain.Where(c => c.tp_p_guid == pguid && c.tp_p_model_no == modelno && c.tp_p_model_no != null).FirstOrDefault();
                if (item != null)
                {
                   
                    ef.iot_device_task_plain_item_handle.Where(c => c.tpi_tp_guid == item.tp_guid).ExecuteDelete();

                    ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == item.tp_guid).ExecuteUpdate(c => c.SetProperty(p => p.tpi_complete, 1)
                   .SetProperty(p => p.tpi_complete_time, DateTime.Now));

                    ef.iot_device_task_plain.Where(c => c.tp_guid == item.tp_guid).ExecuteUpdate(c => c.SetProperty(p => p.tp_complete, 1)
                     .SetProperty(p => p.tp_complete_time, DateTime.Now));

                }

            }
        }
        public bool saveAll(iot_device_task_plain req)
        {

            //先查一下有没重复提交
            iot_device_task_plain model = ef.iot_device_task_plain.Where(c => c.tp_guid == req.tp_guid).FirstOrDefault();
            if (model == null)
            {
                iot_device_task_plain plainmodelno = ef.iot_device_task_plain.Where(c => c.tp_p_guid == req.tp_p_guid).OrderByDescending(c => c.tp_p_model_no).FirstOrDefault();

                if (plainmodelno != null && plainmodelno.tp_p_model_no != null)
                {
                    req.tp_p_model_no = plainmodelno.tp_p_model_no + 1;
                }
                else
                {
                    req.tp_p_model_no = 1;
                }
                //写入
                req.tp_add_time = DateTime.Now;
                req.tp_mod_time = DateTime.Now;
                req.tp_complete = 1;
                req.tp_auto_run = 0;//0启动，1不启动
                req.tp_auto_run_join = 0;// 0不参与自动配置，1参与自动配置
                if (!string.IsNullOrEmpty(req.tp_p_guid))
                {
                    req.tp_is_template = 2;
                    req.tp_c_guid = ef.iot_product.Where(c => c.p_guid == req.tp_p_guid).FirstOrDefault().p_c_guid;
                }
                ef.iot_device_task_plain.Add(req);
                ef.SaveChanges();
            }
            else
            {

                model.tp_title = req.tp_title;
                model.tp_status = req.tp_status;
                model.tp_mod_time = DateTime.Now;
                ef.iot_device_task_plain.Update(model);
                ef.SaveChanges();
            }

            return true;
        }
        public bool changeStatus(string tp_guid)
        {
            iot_device_task_plain model = ef.iot_device_task_plain.Where(c => c.tp_guid == tp_guid).FirstOrDefault();
            if (model != null)
            {
                if (model.tp_status == 0)
                {
                    model.tp_status = 1;
                }
                else
                {
                    model.tp_status = 0;
                }
                ef.SaveChanges();
            }
            return true;
        }
        public void del(string tp_guid)
        {
            ef = new EFYihongIotContext();
            ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == tp_guid).ExecuteDelete();
            ef.iot_device_task_plain.Where(c => c.tp_guid == tp_guid).ExecuteDelete();

        }



    }
}
